package com.zhr.liquwang.view.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.zhr.liquwang.R;
import com.zhr.liquwang.entity.Shop;
import com.zhr.liquwang.view.adapter.GoodsAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.finalteam.okhttpfinal.BaseHttpRequestCallback;
import cn.finalteam.okhttpfinal.HttpRequest;
import cn.finalteam.okhttpfinal.RequestParams;

import static com.zhr.liquwang.api.UrlManager.BASE_SHOP_URL;

public class GoodsActivity extends AppCompatActivity {
    private List<Shop> data;
    @BindView(R.id.iv_goods_back)
    ImageView ivGoodsBack;
    @BindView(R.id.iv_goods_more)
    ImageView ivGoodsMore;
    @BindView(R.id.et_goods)
    EditText etGoods;
    @BindView(R.id.rv_goods)
    RecyclerView rvGoods;
    GoodsAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods);
        initView();
        initData();
    }

    private void initView() {
        ButterKnife.bind(this);
        data = new ArrayList<>();
        rvGoods.setLayoutManager(new GridLayoutManager(this, 2));
        adapter = new GoodsAdapter(this, data);
        rvGoods.setAdapter(adapter);
    }

    private void initData() {
        int shopId = getIntent().getIntExtra("shopid", 0);
        RequestParams params = new RequestParams();
        params.addFormDataPart("shopid", shopId);
        HttpRequest.post(BASE_SHOP_URL, params, new BaseHttpRequestCallback<Shop>() {
            @Override
            protected void onSuccess(Shop shop) {
                super.onSuccess(shop);
                if (shop.getShopId() != 0) {
                    data.add(shop);
                    adapter.notifyDataSetChanged();
                }
            }
        });
    }

    @OnClick({R.id.iv_goods_back, R.id.iv_goods_more, R.id.et_goods})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_goods_back:
                this.finish();
                break;
            case R.id.iv_goods_more:
                break;
            case R.id.et_goods:
                break;
        }
    }
}
